﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Encryption
{
    public partial class CaesarForm : Form
    {
        Random rand;
        int KValue;
        Dictionary<char, char> dic;
        public CaesarForm()
        {
            InitializeComponent();
        }
        private void CaesarForm_Load(object sender, EventArgs e)
        {            
            rand = new Random();
            dic = new Dictionary<char,char>();
            for (int i = 0; i < 26; ++i )
            {
                dic[(char)(i + 'a')] = (char)0;
            }
        }
        private void RanKBtn_Click(object sender, EventArgs e)
        {
            KValue = rand.Next(1,25);
            KValueTex.Text = KValue.ToString();
        }

        private void EncodeBtn_Click(object sender, EventArgs e)
        {
            try{            
                KValue = int.Parse(KValueTex.Text);            
            }
            catch (Exception err)
            {
                 MessageBox.Show("请输入正确的K值","K值错误");
                 Console.WriteLine(err.Message);
                 return;
            }
            if (!(KValue >= 0 && KValue<=26))
            {
                MessageBox.Show("请输入正确的K值", "K值错误");
                return;
            }
            ruleTex1Lab.Text = "";
            ruleTex2Lab.Text = "";
            //生成规则
            for (int i = 0; i < 26; ++i)
            {
                int temp = (i + KValue)%26;
                dic[(char)(i + 'a')] = (char)('a'+temp);
                if(i < 13)
                    ruleTex1Lab.Text = ruleTex1Lab.Text + "\n" + (char)('a' + i) + "  :  " + dic[(char)(i + 'a')];
                else
                    ruleTex2Lab.Text = ruleTex2Lab.Text + "\n" + (char)('a' + i) + "  :  " + dic[(char)(i + 'a')];
            }

            string clearString = encodeTex.Text.ToString();
            string cipherString = "";
            cipherString = encode(clearString);
            decodeTex.Text = cipherString;
        }
        private void DecodeBtn_Click(object sender, EventArgs e)
        {
            try{            
                KValue = int.Parse(KValueTex.Text);            
            }
            catch (Exception err)
            {
                 MessageBox.Show("请输入正确的K值","K值错误");
                 Console.WriteLine(err.Message);
                 return;
            }
            if (!(KValue >= 0 && KValue<=26))
            {
                MessageBox.Show("请输入正确的K值", "K值错误");
                return;
            }
            ruleTex1Lab.Text = "";
            ruleTex2Lab.Text = "";
            //生成规则
            for (int i = 0; i < 26; ++i)
            {
                int temp = (i + KValue)%26;
                dic[(char)(i + 'a')] = (char)('a'+temp);
                if(i < 13)
                    ruleTex1Lab.Text = ruleTex1Lab.Text + "\n" + (char)('a' + i) + "  :  " + dic[(char)(i + 'a')];
                else
                    ruleTex2Lab.Text = ruleTex2Lab.Text + "\n" + (char)('a' + i) + "  :  " + dic[(char)(i + 'a')];
            }

            string clearString = decodeTex.Text.ToString();
            string cipherString = "";
            cipherString = decode(clearString);
            encodeTex.Text = cipherString;
        }
        private string decode(string inputString )
        {
            string OutputString = "";
            foreach(var s in inputString)
            {
                char key = (char)0;
                foreach(var c in dic)
                {
                    if(c.Value == s)
                        key = c.Key;
                }
                OutputString += key;
            }
            return OutputString;
        }
        private string encode(string inputString )
        {
            string OutputString = "";
            foreach (var c in inputString)
            {
                var temp = dic[(char)c];
                OutputString += temp;
            }
            return OutputString;
        }
    }
}
